﻿unit BillScan;

interface

uses
  Winapi.Windows, Winapi.Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, uniGUITypes, uniGUIAbstractClasses, System.StrUtils,
  uniGUIClasses, uniGUIFrame, uniEdit, DB, DBClient, ClientRowSet,
  uniScreenMask, uniBasicGrid, uniDBGrid, uniDBComboBox, uniDBLookupComboBox,
  uniMultiItem, uniComboBox, uniButton, uniBitBtn, uniLabel, uniGUIBaseClasses,
  uniPanel, Clipbrd, uniMemo;

type
  TFrmBillScan = class(TUniFrame)
    UniPanel1: TUniPanel;
    UniLabel5: TUniLabel;
    btSave: TUniBitBtn;
    UniLabel3: TUniLabel;
    UniLabel6: TUniLabel;
    cbNode: TUniDBLookupComboBox;
    GridBillScan: TUniDBGrid;
    dsOrder: TDataSource;
    ScreenMask: TUniScreenMask;
    crsOrder: TClientRowSet;
    dsComp: TDataSource;
    dsEmp: TDataSource;
    dsNode: TDataSource;
    edBillNo: TUniEdit;
    crsTrance: TClientRowSet;
    dsTrance: TDataSource;
    UniLabel1: TUniLabel;
    cbEmployee: TUniDBLookupComboBox;
    lbInfo: TUniPanel;
    cbOperat: TUniDBLookupComboBox;
    dsState: TDataSource;
    crsOrderORDER_ID: TStringField;
    crsOrderCON_ORDID: TStringField;
    crsOrderBILL_NO: TStringField;
    crsOrderORDER_NO: TStringField;
    crsOrderORDER_TYPE: TStringField;
    crsOrderTSP_STATE: TIntegerField;
    crsOrderACPT_STATE: TIntegerField;
    crsOrderCARGO: TStringField;
    crsOrderCARGO_CNT: TIntegerField;
    crsOrderWEIGHT: TFloatField;
    crsOrderVOLUME: TStringField;
    crsOrderDISTANCE: TFloatField;
    crsOrderCOD: TFloatField;
    crsOrderCOD_FEE: TFloatField;
    crsOrderCOD_BLCID: TStringField;
    crsOrderCOD_STATE: TIntegerField;
    crsOrderASK_RTBILL: TIntegerField;
    crsOrderSEND_PNTNO: TStringField;
    crsOrderSEND_GROUP: TStringField;
    crsOrderSEND_NODE: TStringField;
    crsOrderSEND_NAME: TStringField;
    crsOrderSEND_TEL: TStringField;
    crsOrderSEND_CONT: TStringField;
    crsOrderSEND_CITY: TStringField;
    crsOrderSEND_ADDR: TStringField;
    crsOrderDEST_PNTNO: TStringField;
    crsOrderDEST_GROUP: TStringField;
    crsOrderDEST_NODE: TStringField;
    crsOrderDEST_NAME: TStringField;
    crsOrderDEST_TEL: TStringField;
    crsOrderDEST_CONT: TStringField;
    crsOrderDEST_CITY: TStringField;
    crsOrderDEST_ADDR: TStringField;
    crsOrderRECEIVER: TStringField;
    crsOrderTSP_ACCNO: TStringField;
    crsOrderTSP_PNTNO: TStringField;
    crsOrderTSP_GROUP: TStringField;
    crsOrderTSP_NODE: TStringField;
    crsOrderTSP_NAME: TStringField;
    crsOrderTSP_EMPTEL: TStringField;
    crsOrderTRUCK_NO: TStringField;
    crsOrderTSP_TEL: TStringField;
    crsOrderTSP_EMPNO: TStringField;
    crsOrderTSP_EMPNAME: TStringField;
    crsOrderTSP_USRNO: TStringField;
    crsOrderFETCH_NODE: TStringField;
    crsOrderFETCH_EMPNO: TStringField;
    crsOrderFETCH_EMPNAME: TStringField;
    crsOrderFETCH_EMPTEL: TStringField;
    crsOrderFETCH_NODE_INC: TFloatField;
    crsOrderDISP_NODE: TStringField;
    crsOrderDISP_EMPNO: TStringField;
    crsOrderDISP_EMPNAME: TStringField;
    crsOrderDISP_EMPTEL: TStringField;
    crsOrderDISP_NODE_INC: TFloatField;
    crsOrderRECBILL_NAME: TStringField;
    crsOrderFEE_BLCID: TStringField;
    crsOrderFEE: TFloatField;
    crsOrderPAYSIDE: TIntegerField;
    crsOrderADV_FEE: TFloatField;
    crsOrderINUS_FEE: TFloatField;
    crsOrderFEE_STATE: TIntegerField;
    crsOrderDLIV_PAYIN: TFloatField;
    crsOrderBACK_FEE: TFloatField;
    crsOrderCRT_DATE: TStringField;
    crsOrderSND_DATE: TStringField;
    crsOrderARV_DATE: TStringField;
    crsOrderRCV_TIME: TStringField;
    crsOrderEXCEPT_INFO: TStringField;
    crsOrderREMARK: TStringField;
    crsOrderGROUP_CODE: TStringField;
    crsOrderNODE_NO: TStringField;
    crsOrderNODE_NAME: TStringField;
    crsOrderSYS_ACCOUNT: TStringField;
    crsOrderUPDATETIME: TStringField;
    crsOrderVERSION: TLargeintField;
    UniLabel2: TUniLabel;
    cbTruck: TUniDBLookupComboBox;
    UniLabel4: TUniLabel;
    cbBatchNo: TUniDBLookupComboBox;
    edTruck: TUniEdit;
    UniLabel7: TUniLabel;
    edRemark: TUniEdit;
    dsWorkClass: TDataSource;
    edBatchNo: TUniEdit;
    dsTruck: TDataSource;
    crsTranceID: TStringField;
    crsTranceBILL_ID: TStringField;
    crsTranceBILL_NO: TStringField;
    crsTranceDIRECTION: TIntegerField;
    crsTranceTRACK_NO: TStringField;
    crsTrancePKG_NO: TStringField;
    crsTranceOP_CODE: TStringField;
    crsTranceOPERTION: TStringField;
    crsTranceOP_TIME: TStringField;
    crsTranceREC_TIME: TStringField;
    crsTranceGROUP_CODE: TStringField;
    crsTranceNODE_NO: TStringField;
    crsTranceNODE_NAME: TStringField;
    crsTranceNODE_TEL: TStringField;
    crsTranceEMP_NO: TStringField;
    crsTranceEMP_NAME: TStringField;
    crsTranceBATCH_NO: TStringField;
    crsTranceUSER_NO: TStringField;
    crsTranceREMARK: TStringField;
    crsTranceSYNC_FLAG: TStringField;
    crsTranceSYS_ACCOUNT: TStringField;
    crsTranceVERSION: TIntegerField;
    UniLabel8: TUniLabel;
    edRecv: TUniEdit;
    UniLabel9: TUniLabel;
    cbNextNode: TUniDBLookupComboBox;
    crsTranceNEXT_NODE: TStringField;
    crsTranceNEXT_NAME: TStringField;
    UniLabel10: TUniLabel;
    edtTruckNo: TUniEdit;
    crsTranceTRUCK_NO: TStringField;
    crsTranceCARGO_CNT: TIntegerField;
    UniLabel11: TUniLabel;
    edtqty: TUniEdit;
    pnlClose: TUniPanel;
    btClose: TUniBitBtn;
    crsNextNode: TClientRowSet;
    dsNextNode: TDataSource;
    crsNode: TClientRowSet;
    crsEmp: TClientRowSet;
    cmbRemark: TUniComboBox;
    btnSaveBat: TUniBitBtn;
    crsTranceOP_SOURCE: TStringField;
    procedure UniFrameCreate(Sender: TObject);
    procedure crsTranceAfterInsert(DataSet: TDataSet);
    procedure btSaveClick(Sender: TObject);
    procedure edBillNoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure GridBillScanDblClick(Sender: TObject);
    procedure cbBatchNoCloseUp(Sender: TObject);
    procedure cbTruckCloseUp(Sender: TObject);
    procedure cbOperatCloseUp(Sender: TObject);
    procedure cbNextNodeKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure GridBillScanColumnSort(Column: TUniDBGridColumn;
      Direction: Boolean);
    procedure GridBillScanCellClick(Column: TUniDBGridColumn);
    procedure btCloseClick(Sender: TObject);
    procedure btnSaveBatClick(Sender: TObject);
  private
    { Private declarations }
    currField: TField;
    procedure SendArivSMS(billno:String);
    procedure SendRecvSMS(billno: String);
    function checkTruckNo(truckNo: string): Boolean;
    function CheckBillNo(billNo: string): Boolean;
    procedure DoSave(AQty: Integer);
    function billNoIsSign(billNo: string): Boolean;
    function SignNode_NextNode(billNo: string): Boolean;
    procedure InsertTransSendOut(billNo, transId: string; opCode: Integer);
    procedure GetProblemList;
  public
    { Public declarations }
    procedure UpdateBillState;
  end;

implementation
uses ExpModule,uniGUIVars,ServerModule,MainModule,main, uBillScanBat,
     DictModule, EditTspOrder,uCommFunction,EditTranceRmk,ToolModule, MessageBox;
{$R *.dfm}



function TFrmBillScan.billNoIsSign(billNo: string): Boolean;
var
  SQLText: string;
begin
  Result := False;
  SQLText := 'SELECT COUNT(1)cnt FROM tsp_trance WHERE group_code=' + QuotedStr(dmExpress.UserInfo.SysAccount) +
    ' AND bill_no=' + QuotedStr(billNo) + ' AND op_code=''9''';
  dmExpress.crsExec.Close;
  dmExpress.crsExec.CommandText := SQLText;
  dmExpress.crsExec.Open;
  Result := dmExpress.crsExec.FieldByName('cnt').AsInteger > 0;

end;

procedure TFrmBillScan.btCloseClick(Sender: TObject);
begin
  MainForm.CloseOneTab(Self.Hint);
  //MainForm.CloseOneTab('运单扫描');
end;

procedure TFrmBillScan.btnSaveBatClick(Sender: TObject);
var
  billNos: Integer;
begin
  if cbEmployee.Text = '' then
  begin
    FrmMessageBox.ShowInfo(GetShowMsg('0083', '作业人员不能为空，请选择'));
    Exit;
  end;

  if cbOperat.Text = '' then
  begin
    FrmMessageBox.ShowInfo(GetShowMsg('0084', '操作类别不能为空，请选择'));
    cbOperat.SetFocus;
    Exit;
  end;

  with FrmBillScanBat do
  begin
    opCode := cbOperat.KeyValue;
    cargoCnt := 0;
    TryStrToInt(edtqty.Text, cargoCnt);
    opertion := cbOperat.Text;
    nodeNo := cbNode.KeyValue;
    nodeName := cbNode.Text;
    empNo := cbEmployee.KeyValue;
    empName := cbEmployee.Text;
    remark := cmbRemark.Text;
    truckNo := edtTruckNo.Text;
    batchNo := edBatchNo.Text;
    trackNo := edTruck.Text;
    nextNode := '';
    nextName := '';
    if VarToStr(cbNextNode.KeyValue) <> '' then
    begin
      nextNode := cbNextNode.KeyValue;
      nextName := cbNextNode.Text;
    end;
    receiver := edRecv.Text;
    crsFrom := Self.crsTrance;
    ShowModal;
  end;
end;

procedure TFrmBillScan.btSaveClick(Sender: TObject);
var
  cargoCtn: Integer;
  SQLText: string;
begin
  try
    if dmExpress.UserInfo.isPcModel then
    begin
      btSave.Enabled := False;
      btnSaveBat.Enabled := False;
      btClose.Enabled := False;
    end;
    if cbEmployee.Text = '' then
    begin
      FrmMessageBox.ShowInfo(GetShowMsg('0083', '作业人员不能为空，请选择'));
      Exit;
    end;

    if cbOperat.Text = '' then
    begin
      FrmMessageBox.ShowInfo(GetShowMsg('0084', '操作类别不能为空，请选择'));
      cbOperat.SetFocus;
      Exit;
    end;

    if edBillNo.Text = '' then
    begin
      FrmMessageBox.ShowInfo(GetShowMsg('0085', '货运单号不能为空，请输入'));
      edBillNo.SetFocus;
      Exit;
    end;

    if not CheckBillNoValidte(edBillNo.Text) then
    begin
      FrmMessageBox.ShowInfo(GetShowMsg('0139', '请输入有效的运单号!'));
      edBillNo.SetFocus;
      Exit;
    end;

    if cbOperat.KeyValue = 9 then
    if not SignNode_NextNode(edBillNo.Text) then
    begin
      FrmMessageBox.ShowInfo(GetShowMsg('0129', '签收站点与运单目的站点不符, 不能签收!'));
      Exit;
    end;

    if dmExpress.UserInfo.billSignAfterOp = '0' then
    begin
      SQLText := 'SELECT tsp_state FROM tsp_order WHERE tsp_group=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
        ' AND bill_no=' + QuotedStr(edBillNo.Text);
      dmExpress.OpenDataset(dmExpress.crsExec, SQLText);
      if dmExpress.crsExec.FieldByName('tsp_state').AsInteger = 9 then
      begin
        FrmMessageBox.ShowInfo(GetShowMsg('0138', '运单已经签收，不能再进行任何操作!'));
        Exit;
      end;
      if dmExpress.crsExec.FieldByName('tsp_state').AsInteger = 7 then
      begin
        FrmMessageBox.ShowInfo(GetShowMsg('0144', '运单已经退货，不能再进行任何操作!'));
        Exit;
      end;
    end;

    {if not CheckBillNo(edBillNo.Text) then
    begin
      FrmMessageBox.ShowInfo(GetShowMsg('0095', '货运单号不存在，请重新输入!'));
      edBillNo.SetFocus;
      Exit;
    end;  }
    {if (cbOperat.KeyValue = 10) or (cbOperat.KeyValue = 11) then
    begin
      if Trim(edtTruckNo.Text) = '' then
      begin
        FrmMessageBox.ShowInfo(GetShowMsg('0086', '请输入跟车单号!'));
        edtTruckNo.SetFocus;
        Exit;
      end;
      if not checkTruckNo(edtTruckNo.Text) then
      begin
        FrmMessageBox.ShowInfo(GetShowMsg('0087', '车标不存在!'));
        Exit;
      end;
    end; }
    if Trim(edtqty.Text) = '' then
      cargoCtn := 0
    else
    if not TryStrToInt(edtqty.Text, cargoCtn) then
    begin
      FrmMessageBox.ShowInfo(GetShowMsg('0094', '请输入有效的件数!'));
      Exit;
    end;
    {if billNoIsSign(edBillNo.Text) then
    begin
      FrmMessageBox.ShowInfo(GetShowMsg('0100', '此运单已客户签收,不能再进行此类操作!'));
      Exit;
    end;  }

    if cbOperat.KeyValue = 11 then   //卸车时，判断与上一站设置的下一站点是否相符
    begin
      with dmExpress.crsExec do
      begin
        Close;
        CommandText := 'select NEXT_NODE from TSP_TRANCE where GROUP_CODE=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
          ' and BILL_NO=' + QuotedStr(edBillNo.Text) + ' and OP_CODE=10' +
          ' order by OP_TIME desc';
        Open;
        if (FieldbyName('NEXT_NODE').AsString <> cbNode.KeyValue) and (FieldbyName('NEXT_NODE').AsString <> '') then
        begin
          MessageDlg('卸车站点与上一站点所填入的下一站点不相符, 是否继续 ?', mtConfirmation, mbYesNo,
            procedure(Res: Integer)
            begin
              if Res <> mrYes then
              begin
                crsTrance.Cancel;
                crsTrance.CancelUpdates;
                Exit;
              end
              else
              begin
                DoSave(cargoCtn);
              end;
            end
          );
        end
        else
        begin
          DoSave(cargoCtn);
        end;
      end;
    end
    else
    begin
      DoSave(cargoCtn);
    end;
  finally
    if dmExpress.UserInfo.isPcModel then
    begin
      btSave.Enabled := True;
      btnSaveBat.Enabled := True;
      btClose.Enabled := True;
    end;
  end;
end;

procedure TFrmBillScan.cbBatchNoCloseUp(Sender: TObject);
begin
  edBatchNo.Text := cbBatchNo.Text;
end;

procedure TFrmBillScan.cbNextNodeKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key in[vk_DELETE, vk_BACK] then
  if Sender is TUniDBLookupComboBox then
    (Sender as TUniDBLookupComboBox).ItemIndex := -1;
end;

{0	待确认
1	已收件
2	收件入仓
3	问题件
4	发货出仓
5	到货入仓
6	派件出仓
7	已退货
8	滞留入仓
9	客户签收
10 装车
11 卸车
12 拒收}

procedure TFrmBillScan.cbOperatCloseUp(Sender: TObject);
var
  optcode:Integer;
begin
  {if cbOperat.ItemIndex = -1 then
    Exit; }
  if cbOperat.Text = '' then
    Exit;
  optcode := 0;
  optcode := cbOperat.KeyValue;
  edRecv.Enabled := optcode = 9;
  cbTruck.ItemIndex := -1;
  edTruck.Clear;
  cbTruck.Enabled := optcode in [10, 11];
  edTruck.Enabled := cbTruck.Enabled;

  if edRecv.Enabled then
    edRecv.Color := clWindow
  else
    edRecv.Color := clmenu;

  if edTruck.Enabled then
    edTruck.Color := clWindow
  else
    edTruck.Color := clmenu;


end;

procedure TFrmBillScan.cbTruckCloseUp(Sender: TObject);
begin
  edtruck.Text := cbTruck.Text;
end;

function TFrmBillScan.CheckBillNo(billNo: string): Boolean;
begin
  Result := False;
  with dmExpress.crsExec do
  begin
    Close;
    CommandText := 'SELECT bill_no FROM tsp_order WHERE tsp_group=' + QuotedStr(dmExpress.UserInfo.GroupCode) + ' AND bill_no=' + QuotedStr(billNo) +
      ' UNION ALL ' +
      ' SELECT sub_bill_no FROM tsp_order_detail WHERE group_code=' + QuotedStr(dmExpress.UserInfo.GroupCode) + ' AND sub_bill_no=' + QuotedStr(billNo);
    Open;
    Result := RecordCount > 0;
  end;
end;

function TFrmBillScan.checkTruckNo(truckNo: string): Boolean;
begin
  Result := False;
  with dmExpress.crsExec do
  begin
    Close;
    CommandText := 'select count(1) cnt from TSP_TCKBILL where GROUP_CODE=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
      ' and TRUCK_NO=' + QuotedStr(truckNo);
    Open;
    Result := FieldbyName('cnt').AsInteger > 0;
  end;
end;

procedure TFrmBillScan.crsTranceAfterInsert(DataSet: TDataSet);
begin
  DataSet.FieldByName('ID').AsString := getguid;
  DataSet.FieldByName('BILL_ID').AsString := dmexpress.UserInfo.SysAccount+edBillNo.Text;
  DataSet.FieldByName('BILL_NO').AsString := edBillNo.Text;
  DataSet.FieldByName('OP_CODE').AsInteger := cboperat.KeyValue;
  DataSet.FieldByName('OPERTION').AsString := cboperat.Text;
  DataSet.FieldByName('OP_TIME').AsString := formatdatetime('yyyy-MM-dd HH:mm:ss',now);
  DataSet.FieldByName('REC_TIME').AsString := formatdatetime('yyyy-MM-dd HH:mm:ss',now);
  DataSet.FieldByName('GROUP_CODE').AsString := dmexpress.UserInfo.GroupCode;
  DataSet.FieldByName('NODE_NO').AsString :=  cbNode.KeyValue; //dmexpress.UserInfo.NodeNo;
  DataSet.FieldByName('NODE_NAME').AsString := cbNode.Text; //dmexpress.UserInfo.NodeName;
  DataSet.FieldByName('NODE_TEL').AsString := dmexpress.UserInfo.TEL;

  DataSet.FieldByName('EMP_NO').AsString := cbEmployee.KeyValue;
  DataSet.FieldByName('EMP_NAME').AsString := cbEmployee.Text;

  DataSet.FieldByName('REMARK').AsString := cmbRemark.Text;
  DataSet.FieldByName('TRACK_NO').AsString := edTruck.Text;
  DataSet.FieldByName('BATCH_NO').AsString := edBatchNo.Text;

  DataSet.FieldByName('USER_NO').AsString := dmexpress.UserInfo.UserNo;
  DataSet.FieldByName('SYS_ACCOUNT').AsString := dmexpress.UserInfo.SysAccount;
  DataSet.FieldByName('op_source').AsString := 'pc';
  if cbNextNode.Text <> '' then
  begin
    DataSet.FieldByName('next_node').AsString := cbNextNode.KeyValue;
    DataSet.FieldByName('next_name').AsString := cbNextNode.Text;
  end;
  DataSet.FieldByName('truck_no').AsString := edtTruckNo.Text;
  //edRemark.Text := '';
  cmbRemark.Text := '';
end;

procedure TFrmBillScan.DoSave(AQty: Integer);
begin
  crsTrance.Append;
  crsTrancecargo_cnt.AsInteger := AQty;
  crsTrance.ApplyUpdates;
  InsertTransSendOut(crsTrance.FieldByName('bill_no').AsString, crsTranceid.AsString, cbOperat.KeyValue);
  UpdateBillState;
  edBillNo.Text := '';
  edRecv.Text := '';
  //edRemark.Text := '';
  cmbRemark.Text := '';
  edBillNo.SetFocus;
end;

procedure TFrmBillScan.edBillNoKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = 13 then
  begin
    btSave.Click;
  end;
end;

procedure TFrmBillScan.SendArivSMS(billno: String);
var
  smsinfo,enbdate,sqltxt,phoneno,sendname:String;
begin
  enbdate := formatdatetime('yyyy-MM-dd',date-7);
  if (cboperat.KeyValue = 5) and (dmexpress.crsParam.FieldByName('ARV_SMS_ALL').AsInteger = 1) then
  begin
    crsOrder.Close;
    crsOrder.CommandText := 'select * from tsp_order where '
                          + 'tsp_group=' + quotedstr(dmexpress.UserInfo.GroupCode)
                          + ' and bill_no=' + quotedstr(edbillno.Text)
                          + ' and CRT_DATE>=' + quotedstr(enbdate);
    crsOrder.Open;

    if (crsOrder.FieldByName('tsp_state').AsInteger <> 9) and
//       (crsOrder.FieldByName('tsp_state').AsInteger <> 5) and
       (crsOrder.FieldByName('disp_node').AsString = cbnode.KeyValue) then    //签收或已到货的不重发短信
    begin
      phoneno := crsorder.FieldByName('DEST_TEL').AsString;
      sendname := crsorder.FieldByName('SEND_NAME').AsString;
      smsinfo := dmtool.GetSmsInfo(dsorder.DataSet,'DSP_DH');
      if smsinfo = '' then
        smsinfo := '您好，有单号为:'+billno+'的货物已到达:'+cbNode.Text+',准备派送';
      if length(phoneno) = 11 then
        dmtool.SendSMS(phoneno,smsinfo,'到件通知');
    end;
  end;

end;

procedure TFrmBillScan.SendRecvSMS(billno: String);
var
  smsinfo,enbdate,sqltxt,phoneno,rcvpsn:String;
begin
  enbdate := formatdatetime('yyyy-MM-dd',date-7);
  if (cboperat.KeyValue = 9) and (dmexpress.crsParam.FieldByName('ARV_SMS_ALL').AsInteger = 1) then
  begin
    crsOrder.Close;
    crsOrder.CommandText := 'select * from tsp_order where '
                          + 'tsp_group=' + quotedstr(dmexpress.UserInfo.GroupCode)
                          + ' and bill_no=' + quotedstr(edbillno.Text)
                          + ' and CRT_DATE>=' + quotedstr(enbdate);
    crsOrder.Open;

    if (crsOrder.FieldByName('tsp_state').AsInteger <> 9) then
    begin
      phoneno := crsorder.FieldByName('SEND_TEL').AsString;
      rcvpsn  := crsorder.FieldByName('RECEIVER').AsString;

      smsinfo := dmtool.GetSmsInfo(dsorder.DataSet,'DSP_QS');
      if smsinfo = '' then smsinfo := '您好，您发出的单号为:'+billno+'的货物已签收';
      if length(phoneno) = 11 then
        dmtool.SendSMS(phoneno,smsinfo,'到件通知');
    end;
  end;

end;

function TFrmBillScan.SignNode_NextNode(billNo: string): Boolean;
var
  SQLText: string;
begin
  Result := True;
  SQLText := 'SELECT bill_no,disp_node FROM tsp_order WHERE group_code=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
    ' AND bill_no=' + QuotedStr(billNo);
  dmExpress.crsExec.Close;
  dmExpress.crsExec.CommandText := SQLText;
  dmExpress.crsExec.Open;
  if (dmExpress.crsExec.RecordCount = 1) and (dmExpress.crsExec.FieldByName('disp_node').AsString <> '') and (dmExpress.crsExec.FieldByName('disp_node').AsString <> dmExpress.UserInfo.NodeNo) then
    Result := False;
end;

procedure TFrmBillScan.GetProblemList;
begin
  cmbRemark.Items.Clear;

  with dmDict do
  begin
    crsProblem.First;
    while not crsProblem.Eof do
    begin
      if dmExpress.UserInfo.LanguageType = 'ENG' then
        cmbRemark.Items.Add(crsProblem.FieldByName('problem_e_name').AsString)
      else
        cmbRemark.Items.Add(crsProblem.FieldByName('problem_c_name').AsString);
      crsProblem.Next;
    end;
  end;
end;

procedure TFrmBillScan.GridBillScanCellClick(Column: TUniDBGridColumn);
begin
  currField := Column.Field;
end;

procedure TFrmBillScan.GridBillScanColumnSort(Column: TUniDBGridColumn;
  Direction: Boolean);
begin
  SortColumn((GridBillScan.DataSource.DataSet as TClientRowSet), Column.FieldName, Direction);
end;

procedure TFrmBillScan.GridBillScanDblClick(Sender: TObject);
begin
  if currField <> nil then
    Clipboard.AsText := currField.AsString;
  if crsTrance.Active then exit;
  if crsTrance.RecordCount = 0 then exit;
  
  crsTrance.Edit;
  FrmEditTranceRmk.dsTrance.DataSet := crsTrance;
  FrmEditTranceRmk.ShowModal;
end;

procedure TFrmBillScan.InsertTransSendOut(billNo, transId: string; opCode: Integer);
var
  SQLText, orderNo, conOrdId: string;
  sList, eList: TstringList;
  I: Integer;
  SQLs: array of string;
const
  GROUP_CODE = 'SSEXP';
  //GROUP_CODE = 'CS';            //adfafdafafdafd
  FEE_ACCNO_1 = '100100';
  FEE_ACCNO_2 = '100205';

  procedure SplitList(sourceStr: string);
  var
    pIndex: Integer;
  begin
    if sourceStr = '' then
      Exit;
    if RightStr(sourceStr, 1) <> ',' then
      sourceStr := sourceStr + ',';
    sList.Clear;
    while Length(sourceStr) > 0 do
    begin
      pIndex := Pos(',', sourceStr);
      if pIndex > 0 then
      begin
        sList.Add(Copy(sourceStr, 1, pIndex - 1));
        Delete(sourceStr, 1, pIndex);
      end;
    end;
  end;
begin
  SQLText := 'SELECT fee_accno,con_ordid,order_no,is_middle,order_coding FROM tsp_order WHERE tsp_group=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
    ' AND bill_no=' + QuotedStr(billNo);
  dmExpress.crsExec.Close;
  dmExpress.crsExec.CommandText := SQLText;
  dmExpress.crsExec.Open;
  orderNo := dmExpress.crsExec.FieldByName('order_no').AsString;
  conOrdId := dmExpress.crsExec.FieldByName('con_ordid').AsString;
  if dmExpress.crsExec.FieldByName('is_middle').AsString <> '' then  //通过下单接口需要推送路由
  begin
    if opCode in [2, 5, 9]  then
    begin
      SQLText := 'INSERT INTO tsp_trance_sendout(third_trance_id, op_code, doid, channel_id, mailno,tag) ' +
        ' VALUES(' +
        QuotedStr(transId) +
        ',' + IntToStr(opCode) +
        ',' + QuotedStr(dmExpress.crsExec.FieldByName('order_coding').AsString) +
        ',''transport_app''' +
        ',' + QuotedStr(orderNo) +
        ',''middle''' +
        ')';
      dmExpress.crsExec.Close;
      dmExpress.crsExec.CommandText := SQLText;
      dmExpress.crsExec.Execute;
    end;
  end
  else
  begin
    if dmExpress.UserInfo.GroupCode <> GROUP_CODE then  //只处理海南顺速
      Exit;
    if (dmExpress.crsExec.FieldByName('fee_accno').AsString <> FEE_ACCNO_1) and (dmExpress.crsExec.FieldByName('fee_accno').AsString <> FEE_ACCNO_2) then
      Exit;
    try
      sList := TStringList.Create;
      SplitList(conOrdId);
      SetLength(SQLs, sList.Count);
      if opCode in [2, 10, 5, 6, 9]  then
      begin
        for I := 0 to sList.Count - 1 do
        begin
          SQLText := 'INSERT INTO tsp_trance_sendout(third_trance_id, op_code, doid, channel_id, mailno, tag) ' +
            ' VALUES(' +
            QuotedStr(transId) +
            ',' + IntToStr(opCode) +
            ',' + QuotedStr(sList[I]) +
            ',''transport_app''' +
            ',' + QuotedStr(orderNo) +
            ',''kejie''' +
            ')';
          SQLs[I] := SQLText;
        end;
        if sList.Count > 0 then
        begin
          dmExpress.crsExec.Close;
          dmExpress.crsExec.Execute(SQLs);
        end;
      end;
    finally
      SetLength(SQLs, 0);
      FreeAndNil(sList);
    end;
  end;
end;

procedure TFrmBillScan.UniFrameCreate(Sender: TObject);
begin
  SetCompoentLanguage(Self);
  crsNode.Data := dmDict.crsNode.Data;
  cbNode.KeyValue := dmexpress.UserInfo.NodeNo;
  cbNode.Enabled := dmexpress.UserInfo.IsCenter;
  crsNextNode.Data := dmDict.crsNode.Data;
  GetProblemList;
  if dmExpress.UserInfo.LanguageType = 'ENG' then
  begin
    cbOperat.ListField := 'CAPTIONENG';
  end;


  crsTrance.Close;
  crsTrance.CommandText := 'select * from tsp_trance where 1=2';
  crsTrance.Open;

  edTruck.Enabled := False;
  cbTruck.Enabled := False;
  edTruck.Text := '';
  edBatchNo.Text := '';
  edtqty.Text := '';
  SetFieldsSort(GridBillScan);
  currField := nil;
  pnlClose.Visible := dmExpress.UserInfo.isPcModel;

  crsEmp.Data := dmDict.crsEmployee.Data;
  crsEmp.Filter := 'state<>1';
  crsEmp.Filtered := True;
end;

{0	待确认
1	已收件
2	收件入仓
3	拒收
4	发货出仓
5	到货入仓
6	派件出仓
7	已退货
8	滞留入仓
9	客户签收
10 装车
11 卸车}
procedure TFrmBillScan.UpdateBillState;
var
  sqltxt_1, sqltxt_2,enbdate,rcvtime, masterBillNo, SQLText: string;
  opcode:Integer;
  SQLs: array of string;

  function GetMsterBillNo: string;
  begin
    with dmExpress.crsExec do
    begin
      Close;
      CommandText := 'select BILL_NO from TSP_ORDER_DETAIL where GROUP_CODE=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
        ' and SUB_BILL_NO=' + QuotedStr(edBillNo.Text);
      Open;
      Result := FieldbyName('bill_no').AsString;
    end;
  end;

  procedure InsertBill;
  var
    SQLText: string;
  begin
    with dmExpress.crsExec do
    begin
      SQLText := 'SELECT bill_no FROM tsp_order WHERE bill_no=' + QuotedStr(edBillNo.Text) +
        ' AND group_code=' + QuotedStr(dmExpress.UserInfo.GroupCode);
      Close;
      CommandText := SQLText;
      Open;
      if FieldbyName('bill_no').AsString = '' then
      begin
        SQLText := 'INSERT INTO tsp_order(' +
          ' order_id,bill_no,order_no,tsp_state,acpt_state, cargo_cnt, node_no,node_name,group_code,tsp_group';
        if cbOperat.KeyValue = 9 then
          SQLText := SQLText + ',RCV_TIME, RECEIVER';
        SQLText := SQLText + ') VALUES(' +
          QuotedStr(GetGUID) +
          ',' + QuotedStr(edBillNo.Text) +
          ',' + QuotedStr(edBillNo.Text) +
          ',' + cbOperat.KeyValue +
          ',0' +
          ',1' +
          ',' + QuotedStr(dmExpress.UserInfo.NodeNo) +
          ',' + QuotedStr(dmExpress.UserInfo.NodeName) +
          ',' + QuotedStr(dmExpress.UserInfo.GroupCode) +
          ',' + QuotedStr(dmExpress.UserInfo.GroupCode);
         if cbOperat.KeyValue = 9 then
           SQLText := SQLText + ',' + QuotedStr(Formatdatetime('yyyy-MM-dd HH:mm:ss',now)) +
             ',' + QuotedStr(edRecv.Text);
         SQLText := SQLText + ')';
        dmExpress.ExecuteSQL(SQLText);
      end;
    end;
  end;
begin
  try
    InsertBill;
    SetLength(SQLs, 3);
    opcode := cboperat.KeyValue;
    if opcode = 5 then
      self.SendArivSMS(edbillno.Text);
    if opcode = 9 then
      self.SendRecvSMS(edbillno.Text);
    masterBillNo := GetMsterBillNo;

    sqltxt_1 := 'update tsp_order set TSP_STATE=' + Inttostr(opcode) + ',op_source=''pc''';
    sqltxt_2 := 'update TSP_ORDER_DETAIL set TSP_STATE=' + IntToStr(opcode) + ',op_source=''pc''';

    //if (cboperat.Text = '到货入仓') or (cboperat.Text = '收件入仓') or (cboperat.Text = '滞留入仓') then
    if opcode in [2, 5, 8] then
    begin
      sqltxt_1 := sqltxt_1 + ',tsp_node=' + quotedstr(cbNode.KeyValue) + ',tsp_name=' + QuotedStr(cbNode.Text);
      sqltxt_2 := sqltxt_2 + ',tsp_node=' + quotedstr(cbNode.KeyValue) + ',tsp_name=' + QuotedStr(cbNode.Text);
    end;

    //if (cboperat.Text = '派件出仓') or (cboperat.Text = '客户签收') then
    if opcode in [6, 9] then
    begin
      sqltxt_1 := sqltxt_1 + ',disp_node=' + QuotedStr(cbNode.KeyValue) + ' ,DISP_EMPNO=' + QuotedStr(cbEmployee.KeyValue) + ',DISP_EMPNAME=' + QuotedStr(cbEmployee.Text);
      sqltxt_2 := sqltxt_2 + ',disp_node=' + QuotedStr(cbNode.KeyValue) + ' ,DISP_EMPNO=' + QuotedStr(cbEmployee.KeyValue) + ',DISP_EMPNAME=' + QuotedStr(cbEmployee.Text);
    end;

    if (opcode = 4) and (VarToStr(cbNextNode.KeyValue) <> '') then
    begin
      sqltxt_1 := sqltxt_1 + ',disp_node=' + QuotedStr(cbNextNode.KeyValue) + ',disp_empno=' + QuotedStr(cbEmployee.KeyValue) +
        ',disp_empname=' + QuotedStr(cbEmployee.Text);
      sqltxt_2 := sqltxt_2 + ',disp_node=' + QuotedStr(cbNextNode.KeyValue) + ',disp_empno=' + QuotedStr(cbEmployee.KeyValue) +
        ',disp_empname=' + QuotedStr(cbEmployee.Text);
    end;

    if opcode = 1 then
    begin
      SQLText := 'SELECT fetch_empno FROM tsp_order WHERE group_code=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
        ' AND bill_no=' + QuotedStr(edBillNo.Text);
      dmExpress.OpenDataset(dmExpress.crsExec, SQLText);
      if Trim(dmExpress.crsExec.FieldByName('fetch_empno').AsString) = '' then
      begin
        sqltxt_1 := sqltxt_1 + ',fetch_empno=' + QuotedStr(cbEmployee.KeyValue) + ',fetch_empname=' + QuotedStr(cbEmployee.Text);
        sqltxt_2 := sqltxt_2 + ',fetch_empno=' + QuotedStr(cbEmployee.KeyValue) + ',fetch_empname=' + QuotedStr(cbEmployee.Text);
      end;
    end;

    if opcode = 9 then
    begin
      rcvtime := formatdatetime('yyyy-MM-dd HH:mm:ss',now);
      sqltxt_1 := sqltxt_1 + ',RCV_TIME=' + QuotedStr(rcvtime) + ',RECEIVER=' + QuotedStr(edRecv.Text);
      sqltxt_2 := sqltxt_2 + ',RCV_TIME=' + QuotedStr(rcvtime) + ',RECEIVER=' + QuotedStr(edRecv.Text);
    end;

    sqltxt_1 := sqltxt_1 + ' where TSP_GROUP=' + QuotedStr(dmexpress.UserInfo.GroupCode);
    if masterBillNo = '' then
      sqltxt_1 := sqltxt_1 + ' and bill_no=' + QuotedStr(edBillNo.Text)
    else
      sqltxt_1 := sqltxt_1 + ' and (BILL_NO=' + QuotedStr(edBillNo.Text) + ' or BILL_NO=' + QuotedStr(masterBillNo) + ')';
    sqltxt_1 := sqltxt_1 + ' and TSP_STATE<>9';
    sqltxt_2 := sqltxt_2 + ' where TSP_GROUP=' + QuotedStr(dmexpress.UserInfo.GroupCode) + ' and sub_bill_no=' + QuotedStr(edBillNo.Text)
        + ' and TSP_STATE<>9';
    SQLs[0] := 'begin ' + sqltxt_1 + ';end;';
    SQLs[1] := 'begin ' + sqltxt_2 + ';end;';
    SQLs[2] := 'begin UPDATE tsp_order SET last_op_time=' + QuotedStr(crsTranceop_time.AsString) +
      ',remark=' + QuotedStr(cmbRemark.Text) + ' WHERE tsp_group=' + QuotedStr(dmExpress.UserInfo.GroupCode) +
      ' AND (bill_no=' + QuotedStr(edBillNo.Text) + ' OR bill_no=' + QuotedStr(masterBillNo) + ') ;end;';
    try
      //crsOrder.Execute(sqltxt);
      crsOrder.Execute(SQLs);
      lbInfo.Caption := edBillNo.Text + ':[' + cboperat.Text + '] 操作登记成功！';
    except
      lbInfo.Caption := edBillNo.Text + ':更新运单状态无效，单号不存在或已签收，请确认';
    end;
  finally
    SetLength(SQLs, 0);
  end;
end;

end.
